//
//  ScanView.m
//  FZAH
//
//  Created by Yoga on 2017/4/6.
//  Copyright © 2017年 zdqk. All rights reserved.
//

#import "ScanView.h"

@interface ScanView ()
@property (nonatomic, assign) CGFloat cornerLineLength;
@property (nonatomic, strong) UIImageView *scanLine;
@end

@implementation ScanView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _cornerLineLength = 20;
        _scanSize = CGSizeMake(250, 250);
        _scanLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scanLine"]];
        
        self.scanLine.frame = CGRectMake(self.center.x - self.scanSize.width * 0.5, self.center.y - self.scanSize.height * 0.5, self.scanSize.width, 20);
        self.scanLine.hidden = YES;
        [self addSubview:self.scanLine];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self drawScanLayer];
}

- (void)drawScanLayer {
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointZero];
    [path addLineToPoint:CGPointMake(ScreenWidth, 0)];
    [path addLineToPoint:CGPointMake(ScreenWidth, ScreenHeight)];
    [path addLineToPoint:CGPointMake(0, ScreenHeight)];
    [path addLineToPoint:CGPointMake(0, 0.1)];
    
    CGPoint point = CGPointMake(self.center.x - self.scanSize.width * 0.5, self.center.y - self.scanSize.height* 0.5 + 0.1);
    [path addLineToPoint:point];
    [path addLineToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5,
                                     self.center.y + self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5,
                                     self.center.y + self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5,
                                     self.center.y - self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5,
                                     self.center.y - self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(0, 0)];
    [path closePath];
    
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = [UIColor colorWithWhite:0 alpha:0.3].CGColor;
    
    [self.layer addSublayer:shapeLayer];
    
    path = [[UIBezierPath alloc] init];
    path.lineWidth = 0;
    // 左上
    [path moveToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5 + self.cornerLineLength,
                                  self.center.y - self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5,
                                     self.center.y - self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5,
                                     self.center.y - self.scanSize.height * 0.5 + self.cornerLineLength)];
    // 右上
    [path moveToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5 - self.cornerLineLength,
                                  self.center.y - self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5,
                                     self.center.y - self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5,
                                     self.center.y - self.scanSize.height * 0.5 + self.cornerLineLength)];
    // 左上
    [path moveToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5,
                                  self.center.y + self.scanSize.height * 0.5 - self.cornerLineLength)];
    [path addLineToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5,
                                     self.center.y + self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x + self.scanSize.width * 0.5 - self.cornerLineLength,
                                     self.center.y + self.scanSize.height * 0.5)];
    // 左上
    [path moveToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5 + self.cornerLineLength,
                                  self.center.y + self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5,
                                     self.center.y + self.scanSize.height * 0.5)];
    [path addLineToPoint:CGPointMake(self.center.x - self.scanSize.width * 0.5,
                                     self.center.y + self.scanSize.height * 0.5 - self.cornerLineLength)];
    
    UIGraphicsBeginImageContext(self.bounds.size);
    [path stroke];
    UIGraphicsEndImageContext();
    
    shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.path = path.CGPath;
    shapeLayer.strokeColor = [UIColor ThemeColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    
    [self.layer addSublayer:shapeLayer];
}

- (void)startAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
    animation.values = @[@((ScreenHeight - self.scanSize.height - self.scanLine.frame.size.height)/2), @((ScreenHeight + self.scanSize.height - self.scanLine.frame.size.height)/2)];
    animation.duration = 2.0;
    animation.repeatCount = MAXFLOAT;
    [self.scanLine.layer addAnimation:animation forKey:nil];
    self.scanLine.hidden = NO;
}

- (void)stopAnimation {

    [self.scanLine.layer removeAllAnimations];
    self.scanLine.hidden = YES;
}

@end
